package com.qf.java2201.controller;

import com.qf.java2201.common.PageVo;
import com.qf.java2201.common.R;
import com.qf.java2201.pojo.ShopCar;
import com.qf.java2201.service.ShopCarService;
import com.qf.java2201.utils.OssUtil;
import com.qf.java2201.vo.PieVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shopCar")
//@CrossOrigin("*")
public class ShopCarController {
    @Autowired
    private ShopCarService shopCarService;
    @RequestMapping("/shopCarPageData")
    public PageVo<ShopCar> getPageData(@RequestParam Map<String, Object> params) {
        return shopCarService.getPageData(params);
    }

    @RequestMapping("/addShopCar")
    public R addShopCar(ShopCar shopCar) {
        R r = new R();// 响应的转态

        //把shopCar 的数据存到 result中
        int result = shopCarService.addShopCar(shopCar);

       if(result == 1){
           r.setCode(0);//响应的转态 0 代表成功
           r.setData(result);//响应的数据？？？
           r.setMsg("添加成功");
       }else {
           r.setCode(-1);
           r.setMsg("添加失败");
       }

        return r;
    }


    @RequestMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {
        // 上传照片

        String filename = file.getOriginalFilename();  // 得到文件的名字

        InputStream inputStream = file.getInputStream();  // 将file 对象转为 输入流

        // 阿里云存储  返回 上传之后的文件路径
        String path = OssUtil.uploadFile(inputStream,filename);

        R<String> r = new R();
        r.setCode(0);
        r.setMsg("上传成功");
        r.setData(path);
        return r;
    }

    @RequestMapping("/deleteShopCar")
    public R deleteShopCar(@Param("cartid") Integer cartid) {
        R r = new R();

        int result = shopCarService.deleteShopCar(cartid);

        if(result == 1){
            r.setCode(0);//响应的转态 0 代表成功
            r.setData(result);//响应的数据
            r.setMsg("删除成功");
        }else {
            r.setCode(-1);
            r.setMsg("删除失败");
        }

        return r;
    }

    @RequestMapping("/updateShopCar")
    public R updateShopCar(ShopCar shopCar) {
        R r = new R();

        int result = shopCarService.updateShopCar(shopCar);
        if(result == 1){
            r.setCode(0);//响应的转态 0 代表成功
            r.setData(result);//响应的数据
            r.setMsg("修改成功");
        }else {
            r.setCode(-1);
            r.setMsg("修改失败");
        }

        return r;
    }

    @RequestMapping("/echarts2")
    public R echartsPoe() {
        List<PieVo> pieVos = shopCarService.sexPieData();

        R ok = R.ok();
        ok.setData(pieVos);
        return ok;
    }

}
